package com.kotlin.titlebar

import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView

abstract class BaseTitleBar<T : BaseTitleParams>(params : T) : ITitleBar{
    var mParams = params
    var mView : View ?= null

    init {
        bindViewToActivityRoot()
    }

    /**
     * 隐藏View
     */
    protected fun hideView(viewId: Int) {
        val view = getView<View>(viewId)
        view.visibility = View.GONE
    }

    /**
     * 显示布局
     * */
    protected fun showView(viewId: Int){
        val view = getView<View>(viewId)
        view.visibility = View.VISIBLE
    }

    /**
     * 设置标题
     * */
    protected fun setTitleText(viewId: Int, text: String){
        val titleText = getView<TextView>(viewId)
        if (!TextUtils.isEmpty(text)){
            titleText.visibility = View.VISIBLE
            titleText.text = text
        }
    }

    /**
     * 设置标题与标题字体颜色
     * */
    protected fun setTitleText(viewId: Int, text: String, colorRes: Int){
        val titleText = getView<TextView>(viewId)
        if (!TextUtils.isEmpty(text)){
            titleText.visibility = View.VISIBLE
            titleText.text = text
            titleText.setTextColor(colorRes)
        }
    }

    /**
     * 设置点击事件
     *
     * @param viewId
     * @param listener
     */
    protected fun setOnClickListener(viewId: Int, listener: View.OnClickListener) {
        val view = getView<View>(viewId)
        view.setOnClickListener(listener)
    }

    /**
     * 获取view
     *
     * @param viewId
     * @param <V>
     * @return
    </V> */
    open fun <V : View> getView(viewId: Int): V {
        return mView!!.findViewById(viewId)
    }

    /**
     * 绑定View到ActivityRoot上
     */
    private fun bindViewToActivityRoot() {
        var activityRoot: ViewGroup ?= null
        if (activityRoot == null) {
            //获取Activity的根布局
            activityRoot = mParams.mActivity.findViewById<View>(android.R.id.content) as ViewGroup
            //获取我们在Activity中设置ContentView的View
            mParams.mActivityRoot = activityRoot.getChildAt(0) as ViewGroup
        }

        mView = LayoutInflater.from(mParams.mActivity.baseContext).inflate(bindLayoutId(), activityRoot, false)

        if (mParams.mActivityRoot is RelativeLayout){
            //相对布局
            //先构造一个线性布局，指定垂直排列
            var newLinearLayout  = LinearLayout(mParams.mActivity)
            newLinearLayout.orientation = LinearLayout.VERTICAL

            //移除原有的activityRoot的parent，否则会报the specified child already has a parent
            //you must call removeView() on the child's parent first的异常
            var viewParent = mParams.mActivityRoot.parent as ViewGroup?
            viewParent!!.removeAllViews()

            //将titleBar添加为第一个child,原来的activityRoot为第二个
            newLinearLayout.addView(mView, 0)
            newLinearLayout.addView(mParams.mActivityRoot, 1)

            //将新的activityRoot添加到android.R.id.content中
            activityRoot.addView(newLinearLayout, 0)
        }else{
            //添加View到mParams.mParent中
            mParams.mActivityRoot.addView(mView, 0)
        }

        //绑定view
        bindView()
    }

    /**
     * Builder设计模式,主要用于设置各种效果
     */
    abstract class Builder {
        abstract fun build(): BaseTitleBar<*>
    }
}